﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SMBCFamilyAssessment
{


    
    public partial class AssessmentSession : System.Web.UI.Page
    {
        Wizard WizardControlAssessment;

        protected override void OnInit(EventArgs e)
        {

            WizardControlAssessment = new Wizard();
            WizardControlAssessment.ActiveStepChanged+=WizardControlAssessment_ActiveStepChanged;
            WizardControlAssessment.NextButtonClick += WizardControlAssessment_NextButtonClick;
            WizardControlAssessment.PreviousButtonClick += WizardControlAssessment_PreviousButtonClick;
            WizardControlAssessment.FinishButtonClick += WizardControlAssessment_FinishButtonClick;
            WizardControlAssessment.CancelButtonClick+=WizardControlAssessment_CancelButtonClick;
            WizardControlAssessment.DisplaySideBar = false;
            

            
            FillData();


            

            base.OnInit(e);
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {

           
            }



        protected void WizardControlAssessment_ActiveStepChanged(object sender, EventArgs e)
        {

            
            
           


        }
        private void WizardControlAssessment_CancelButtonClick(object sender, EventArgs e)
        {

        }

        protected void WizardControlAssessment_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
        {
            
        }

        protected void WizardControlAssessment_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {

          

            Label lblQuestion =(Label) WizardControlAssessment.WizardSteps[e.CurrentStepIndex].Controls[0];
            Telerik.Web.UI.RadSlider rSlider = (Telerik.Web.UI.RadSlider)WizardControlAssessment.WizardSteps[e.CurrentStepIndex].Controls[1];
            SMBCFamilyAssessment.Logic.AssessmentData data = (SMBCFamilyAssessment.Logic.AssessmentData)ViewState["data"];
            //may need a review

            SMBCFamilyAssessment.Models.AssessmentSession session = new SMBCFamilyAssessment.Models.AssessmentSession();
            session.AssessmentTypeId = 3;
            session.AssessmentSessionDate = DateTime.Now.Date.Date;
            session.CreatedBy = "Admin";
            session.DateCreated = DateTime.Now;
            session.PersonId = 1;
            session.QuestionSetId = data.QuestionAndQuestionSetDictionary[e.CurrentStepIndex+1];
            session.QuestionId = (e.CurrentStepIndex+1);
            session.Score = Convert.ToInt32(rSlider.Value);
            if (ViewState["sessionObjectsDictionary"] != null)
            {
                Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession>
                    sObjects1 = (Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession>)ViewState["sessionObjectsDictionary"];
                if (sObjects1.ContainsKey(e.CurrentStepIndex))
                {
                    sObjects1.Remove(e.CurrentStepIndex);

                    sObjects1.Add((e.CurrentStepIndex), session);

                    ViewState["sessionObjectsDictionary"] = sObjects1;
                }
               
                 Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession>
                     sObjects2 = (Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession>)ViewState["sessionObjectsDictionary"];
                 if (!sObjects2.ContainsKey(e.CurrentStepIndex))


                     sObjects2.Add((e.CurrentStepIndex), session);

                 ViewState["sessionObjectsDictionary"] = sObjects2;

            }

            else if (ViewState["sessionObjectsDictionary"] == null)
            {

                Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession>
                   sObjects3 = new Dictionary<int, Models.AssessmentSession>();
                sObjects3.Add((e.CurrentStepIndex), session);
                ViewState["sessionObjectsDictionary"] = sObjects3;



            }


            
            
            //may need a review
            Debug.Write("Retrieved Question text for the step  : " + e.CurrentStepIndex + " is " + lblQuestion.Text+ Environment.NewLine);
            Debug.Write("Retrieved score value for   : " + e.CurrentStepIndex + "Question is " +rSlider.Value  + Environment.NewLine);

            



            //Debug.Write("Control id is : " + WizardControlAssessment.WizardSteps[e.CurrentStepIndex].Controls.

            
            


        }

        protected void WizardControlAssessment_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {



            Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession> sObjects = (Dictionary<int, SMBCFamilyAssessment.Models.AssessmentSession>) ViewState["sessionObjectsDictionary"];
            
            SMBCFamilyAssessment.Logic.AssessmentBLcs.InsertAssessment(sObjects);




        }



        protected void FillData()
        {
            

            SMBCFamilyAssessment.Logic.AssessmentBLcs bl = new Logic.AssessmentBLcs();

            SMBCFamilyAssessment.Logic.AssessmentData data = bl.GetAssessmentData(1);

            ViewState["data"] = data;

            for (int i = 0; i < data.QuestionDictionary.Count; i++)
            {

                WizardStepBase newStep = new WizardStep();
                newStep.ID = "Step" + (i + 1).ToString();
                WizardControlAssessment.WizardSteps.Add(newStep);

            }

            for (int j = 0; j < WizardControlAssessment.WizardSteps.Count; j++)
            {

                Label lblQuestionText = new Label();
                lblQuestionText.Text = data.QuestionDictionary[j + 1];


                WizardControlAssessment.WizardSteps[j].Controls.Add(lblQuestionText);



                WizardControlAssessment.WizardSteps[j].Controls.Add(GetNewSliderItem(j));


            }


            WizardControlAssessment.Height = Unit.Pixel(150);
            WizardControlAssessment.Width = Unit.Pixel(200);

           
            WizardDiv.Controls.Add(WizardControlAssessment);
        
        }


        public Telerik.Web.UI.RadSlider GetNewSliderItem(int index)
        {


            Telerik.Web.UI.RadSlider rSlider = new Telerik.Web.UI.RadSlider();
            rSlider.Width = 400;
            rSlider.Height = 200;
            rSlider.AnimationDuration = 400;
            rSlider.Orientation = Orientation.Horizontal;
           
            rSlider.MaximumValue = 10;
            rSlider.SmallChange = 1;
            rSlider.LargeChange = 2;
            rSlider.ItemType = Telerik.Web.UI.SliderItemType.Tick;
            rSlider.ThumbsInteractionMode = Telerik.Web.UI.SliderThumbsInteractionMode.Free;

            
            Telerik.Web.UI.RadSliderItem i1 = new Telerik.Web.UI.RadSliderItem();

            i1.Text = "10";
            i1.Value = "10";
            rSlider.Items.Add(i1);
            Telerik.Web.UI.RadSliderItem i2 = new Telerik.Web.UI.RadSliderItem();
            i2.Text = "20";
            
            i2.Value = "20";

            rSlider.Items.Add(i2);
            Telerik.Web.UI.RadSliderItem i3 = new Telerik.Web.UI.RadSliderItem();
            i3.Text = "30";
            i3.Value = "30";
            rSlider.Items.Add(i3);
            Telerik.Web.UI.RadSliderItem i4 = new Telerik.Web.UI.RadSliderItem();
            i4.Text = "40";
            i4.Value = "40";
            rSlider.Items.Add(i4);

            Telerik.Web.UI.RadSliderItem i5 = new Telerik.Web.UI.RadSliderItem();
            i5.Text = "50";
            i5.Value = "50";
            rSlider.Items.Add(i5);
            rSlider.ID = "r" + index.ToString();
           
 



            return rSlider;

        }




            public static Control FindControlRecursive(Control root, string id)
            {
                if (root.ID == id)
                    return root;

                return root.Controls.Cast<Control>()
                   .Select(c => FindControlRecursive(c, id))
                   .FirstOrDefault(c => c != null);
            }

            

            

            

        }

        
            

        

}